﻿using Clancy.Interfaces;
using Ninject;
using ReactiveUI;

namespace Clancy.Ui.ViewModels
{
    public class MainWindowViewModel : RibbonElementConsumingViewModelBase
    {
        private readonly CameraManagerViewModel m_CameraManagerViewModel;
        private readonly OnlineConnectionViewModel m_OnlineConnectionViewModel;

        private readonly OperationModeViewModel m_OperationModeVideModel;
        private readonly PropertyEditorViewModel m_PropertyEditorViewModel;
        private readonly RecordingArchiveViewModel m_RecordingArchiveViewModel;

        private readonly TeachThresholdViewModel m_TeachThresholdViewModel;
        private ObservableAsPropertyHelper<OperationMode> m_OperationModeHelper;

        public MainWindowViewModel(IKernel kernel)
        {
            if (kernel == null)
                return;

            m_OperationModeVideModel = kernel.Get<OperationModeViewModel>();
            m_CameraManagerViewModel = kernel.Get<CameraManagerViewModel>();
            m_OnlineConnectionViewModel = kernel.Get<OnlineConnectionViewModel>();
            m_RecordingArchiveViewModel = kernel.Get<RecordingArchiveViewModel>();
            m_PropertyEditorViewModel = kernel.Get<PropertyEditorViewModel>();
            m_TeachThresholdViewModel = kernel.Get<TeachThresholdViewModel>();

            FillRibbon();

            InitializeProperties(kernel.Get<IOperationalAdapterProvider>());
        }

        public CameraManagerViewModel CameraManagerViewModel
        {
            get { return m_CameraManagerViewModel; }
        }

        public OnlineConnectionViewModel OnlineConnectionViewModel
        {
            get { return m_OnlineConnectionViewModel; }
        }

        public bool OnlineIconsActive
        {
            get { return (m_OperationModeHelper.Value == OperationMode.Online); }
        }

        public PropertyEditorViewModel PropertyEditorViewModel
        {
            get { return m_PropertyEditorViewModel; }
        }

        public RecordingArchiveViewModel RecordingArchiveViewModel
        {
            get { return m_RecordingArchiveViewModel; }
        }

        public TeachThresholdViewModel TeachThresholdViewModel
        {
            get { return m_TeachThresholdViewModel; }
        }

        protected override void DisposeElements()
        {
        }

        private void FillRibbon()
        {
            FillRibbon(m_OperationModeVideModel.RibbonElements);
            FillRibbon(m_OnlineConnectionViewModel.RibbonElements);
            FillRibbon(m_CameraManagerViewModel.RibbonElements);
            FillRibbon(m_RecordingArchiveViewModel.RibbonElements);
            FillRibbon(m_TeachThresholdViewModel.RibbonElements);
        }

        private void InitializeProperties(IOperationalAdapterProvider operationalAdapterProvider)
        {
            m_OperationModeHelper = new ObservableAsPropertyHelper<OperationMode>(operationalAdapterProvider.OperationMode,
                _ => { this.RaisePropertyChanged("OnlineIconsActive"); });
        }
    }
}